package com.olda.play.system.biz.impl;

import com.olda.play.enums.ConstantEnum;
import com.olda.play.system.biz.SysDictionaryItemBizService;
import com.olda.play.system.biz.dao.SysDictionaryItemBizMapper;
import com.olda.play.system.model.dao.SysDictionaryItemMapper;
import com.olda.play.system.model.po.SysDictionaryItem;
import com.olda.play.system.model.po.SysDictionaryItemExample;
import com.olda.play.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 */
@Service
public class SysDictionaryItemBizServiceImpl implements SysDictionaryItemBizService {

    @Autowired
    private SysDictionaryItemMapper sysDictionaryItemMapper;
    @Autowired
    private SysDictionaryItemBizMapper sysDictionaryItemBizMapper;

    /**
     * 根据数据字典编码更新字典子项
     * @param sysDictionaryItem
     * @param dictCode
     */
    @Override
    public void updateSysDictItemByDictCode(SysDictionaryItem sysDictionaryItem, String dictCode) {
        SysDictionaryItemExample example=new SysDictionaryItemExample();
        SysDictionaryItemExample.Criteria criteria=example.createCriteria();
        criteria.andDictCodeEqualTo(dictCode);

        sysDictionaryItem.setModifyTime(new Date());
        sysDictionaryItem.setModifyUser(UserUtils.getUserId().toString());
        sysDictionaryItem.setModifyUserName(UserUtils.getUsername());
        sysDictionaryItemMapper.updateByExampleSelective(sysDictionaryItem,example);

    }

    /**
     * 根据数据字典编码删除所有子项数据
     * @param dictCode
     */;
    @Override
    public void deleteSysDictItemBydictCode(String dictCode) {
     /*   SysDictionaryItemExample example=new SysDictionaryItemExample();
        SysDictionaryItemExample.Criteria criteria=example.createCriteria();
        criteria.andDictCodeEqualTo(dictCode);*/

        SysDictionaryItem sysDictionaryItem=new SysDictionaryItem();
        sysDictionaryItem.setModifyTime(new Date());
        sysDictionaryItem.setModifyUser(UserUtils.getUserId().toString());
        sysDictionaryItem.setModifyUserName(UserUtils.getUsername());
        sysDictionaryItem.setIsDeleted(1);

        updateSysDictItemByDictCode(sysDictionaryItem,dictCode);
//        sysDictionaryItemMapper.updateByExampleSelective(sysDictionaryItem,example);
    }

    /**
     * 根据id查询数据字典子项最新详情
     * @param id
     * @return
     */
    @Override
    public SysDictionaryItem selectSysDictItemById(Long id) {

        return sysDictionaryItemMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据字典字典编码查询字典子项信息
     * @param itemCode
     * @return
     */
    @Override
    public SysDictionaryItem selectSysDictItemByItemCode(String itemCode) {
        SysDictionaryItemExample example=new SysDictionaryItemExample();
        SysDictionaryItemExample.Criteria criteria=example.createCriteria();
        criteria.andItemCodeEqualTo(itemCode);

        List<SysDictionaryItem> list=sysDictionaryItemMapper.selectByExample(example);
        return list.size()>0?list.get(0):null;
    }

    /**
     * 更新数据字典子项
     * @param sysDictionaryItem
     */
    @Override
    public void updateSysDictItemById(SysDictionaryItem sysDictionaryItem) {
        sysDictionaryItem.setModifyTime(new Date());
        sysDictionaryItem.setModifyUser(UserUtils.getUserId().toString());
        sysDictionaryItem.setModifyUserName(UserUtils.getUsername());
        sysDictionaryItem.setIsDeleted(ConstantEnum.IS_DELETE_NO.getCode());
        sysDictionaryItemMapper.updateByPrimaryKeySelective(sysDictionaryItem);
    }
    /**
     * 根据主键删除数据字典子项
     * @param id
     */
    @Override
    public void deleteSysDictItemById(Long id) {
        sysDictionaryItemMapper.deleteByPrimaryKey(id);
    }
}
